'''
Created on Apr 22, 2013

@author: Petko
'''
import unittest
from utils.sententialyzer_client import SententialyzerClient
from urllib2 import HTTPError

class GetPathToOpinionsTest(unittest.TestCase):

    sc = SententialyzerClient()
    
    def setUp(self):
        pass


    def tearDown(self):
        #result = self.sc.viewListOp('all_base_opinions', 'newest', access_token = self.sc.accessToken)
        #page = result['page']
        
        pass

    
            
    def authAndCreateOp(self, parent, evaluation, **kwargs):
        return self.sc.authAndCreateOp(None, parent, evaluation, **kwargs)
    
    def prepare_tree(self):
        self.sc.authorize('moderator1', 'test', 'bopin_c')
        
        self.baseOp = self.sc.createBaseOp("test_get_path_success", "bla bla bla", "moderators:d sent_default_group:rc")
        
        self.op1 = self.authAndCreateOp(self.baseOp['id'], 'positive', title="my comment")
        
        self.op2 = self.authAndCreateOp(self.op1['id'], 'positive', title="my comment")
        
        self.op3 = self.authAndCreateOp(self.op2['id'], 'positive', title="my comment")
        
        self.op12 = self.authAndCreateOp(self.op1['id'], 'positive', title="my comment")
        
        self.op4 = self.authAndCreateOp(self.op3['id'], 'positive', title="my comment")
        
        
    def test_get_path_success(self):
        self.prepare_tree()
        
        path = self.sc.get_path_to_opinion(self.baseOp['id'], self.op4['id'])
        
        self.assertEqual(3, len(path))
        
        self.assertEqual(self.op1['id'], path[0]['id'])
        self.assertEqual(self.op2['id'], path[1]['id'])
        self.assertEqual(self.op3['id'], path[2]['id'])
    
    def test_get_path_not_same(self):
        self.prepare_tree()
        
        self.sc.authorize('moderator1', 'test', 'bopin_c')
        
        baseOp1 = self.sc.createBaseOp("test_get_path_success", "bla bla bla", "moderators:d sent_default_group:rc")
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.get_path_to_opinion(baseOp1['id'], self.op4['id'])
        self.assertEqual(cm.exception.code, 400)
    
    def test_get_path_not_parent(self):
        self.prepare_tree()
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.get_path_to_opinion(self.op12['id'], self.op2['id'])
        self.assertEqual(cm.exception.code, 400)
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.get_path_to_opinion(self.op2['id'], self.op12['id'])
        self.assertEqual(cm.exception.code, 400)
        
        with self.assertRaises(HTTPError) as cm:
            self.sc.get_path_to_opinion(self.op3['id'], self.op2['id'])
        self.assertEqual(cm.exception.code, 400)
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
